<?php
namespace Back\Controller;
use Think\Controller;
class GoodsController extends Controller {
    public function showlist(){
        //$goods = D('Goods');
        //dump($goods); //tp中的
        //var_dump() php的

        $goods = new \Model\GoodsModel();
        $nowinfo = $goods->fetchData();
        $info = $nowinfo['pageinfo'];
        $pagelist = $nowinfo['pagelist'];

        
        //$info = $goods->order('goods_id desc')->select();

        $this->assign('info',$info);
        $this->assign('pagelist',$pagelist);
        

        $this->display();
    }

    public function tianjia(){

        //$goods = D('Goods');//实例化父类Model对象
        //$goods = M('Goods');//实例化父类Model对象

        $goods = new \Model\GoodsModel();

       // $_POST['add_time'] = time();
        //$_POST['upd_time'] = time();

        if (IS_POST){

            //C方法读取配置变量
            //默认过滤方法 DEFAULT_FILTER
            //htmlspecialchars
            $data = $goods->create();

            //消除了不合法的标签
            $data['goods_introduce'] = \fanXSS($_POST['goods_introduce']);

            if ($goods->add($data)){

                //调试时让其不跳转
                $this->success('添加商品成功',U('showlist'),2);
                
                

            }else{

                $this->error('添加商品失败',U('tianjia'),2);
            }

        }else{
            $this->display();
        }

    }

    public function upd(){
        $goods = new \Model\GoodsModel();

        if (IS_POST){
            //dump($_POST);
            $data = $goods->create();

            //消除了不合法的标签
            $data['goods_introduce'] = \fanXSS($_POST['goods_introduce']);

            
            if ($goods->save($data)){
                $this->success('修改商品成功',U('showlist'),2);

            }else{

                $this->error('修改商品失败',U('upd',array('goods_id' => $data['goods_id'])),2);

                //$this->error('修改商品失败',U('upd',array('goods_id' => I('get.goods_id'))),2);
            }
        }else{
            $goods_id = I('get.goods_id');
            //find 一维数组
            $info = $goods->find($goods_id);


            //相册信息
            $picsinfo = D(GoodsPics)->where(array('goods_id' => $goods_id))->select();
            if (!empty($picsinfo)){
                $this->assign('picsinfo',$picsinfo);

                //dump($picsinfo);
            }

            $this->assign('info',$info);

            $this->display();
        }

    }
    
    function delPics(){
        $pics_id = I('get.pics_id');
        $info = D('GoodsPics')->find($pics_id);
        unlink($info['pics_big']);
        unlink($info['pics_small']);
        
        $z = D('GoodsPics')->delete($pics_id);
        if ($z){
            echo "删除成功";
        }
        
        
        
    }

    function delGoods(){
        $goods_id = I('goods_id');
        $goods = D('Goods'); //父类对象,不影响瞻前顾后

        //setField 内部调用save方法 影响瞻前顾后
        $z = $goods->setField(array('goods_id'=>$goods_id,'is_del'=>'删除'));

        //dump($z);
        if ($z){
            echo json_encode(array('status'=>1));
        }else{
            echo json_encode(array('status'=>2));
        }



    }
    

}